<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包用在单例</title>
</head>

<body>
    <button id="add">新增</button>
    <button id="del">删除</button>
    <div id="dialog">

    </div>

</body>
<script>
    let  addBtn = document.getElementById("add");
    let  delBtn =document.getElementById("del");

    function  fn(){
        let  dialog = document.getElementById("dialog");//等价于  let num = 10;

        return  (json)=>{
            dialog.innerHTML = json.html;
            let  btn = document.createElement("button");
            btn.innerHTML ="确定";
            dialog.appendChild(btn);
            btn.onclick =json.btns.success;

        }

    }
    let  Dialog = fn();


    addBtn.onclick =function(){
        Dialog({
            html:`用户<input id="username"/>`,
            btns:{
                success:function(){
                    alert(document.getElementById("username").value);
                }
            }

        });

    }
    delBtn.onclick =function(){
        Dialog({
            html:"你确定要删除我吗？",
            btns:{
                success:function(){
                    alert("删除的操作");
                }
            }
        });

    }

</script>

</html>